$(document).ready(function() {
	//获取页面上的所有元素，并组成一个对象
	var obj = {
		id: $("#name_id"),
		name: $('.name'),
		tel: $('#telphone'),
		Province: $('.county'),
		City: $('.street'),
		Area: $('.detail'),
	}
	//如果有缓存，要进行赋值
	if(localStorage.getItem('data')) {
		var adressData = JSON.parse(localStorage.getItem('data'));
		console.log(adressData);
		obj.name.val(adressData.name);
		obj.id.val(adressData.id);
		obj.tel.val(adressData.tel);
		obj.Province.val(adressData.sheng);
		obj.City.val(adressData.shi);
		obj.Area.val(adressData.qu);
		if(adressData.state == 1) {
			$(".check").prop("checked", true);
		} else if(adressData.state == 2) {
			$(".check").prop("checked", false);
		}
	}
	//点击保存按钮，获取页面上的内容并存到缓存中
	$(".edit_address_header p:nth-child(3)").bind("click", function() {
		var name = $(".name").val();
		var telphone = document.getElementById("telphone").value;
		var area = $(".county").val();
		var address = $(".detail").val();
		var street = $(".street").val();
		var zip_code = $(".zip_code").val();
		var id = $("#name_id").val();
		var state;
		if($(".check").prop("checked") == true) {
			state = 1;
		} else if($(".check").prop("checked") == false){
			state = 2;
		}
		console.log(state);
		//			判断输入框的内容是否为空
		var myreg = /^1[3|4|5|7|8][0-9]\d{4,8}$/;
		if(name == "") {
			console.log("姓名不能为空!");
		} else if(telphone == '') {
			console.log("电话不能为空!");
		} else if(telphone.length != 11) {
			console.log("请输入有效的手机号");
		} else if(!myreg.test(telphone)) {
			console.log("请输入有效的手机号!");
		} else if(area == "") {
			console.log("请输入地区名称");
		} else if(street == "") {
			console.log("请输入街道名称");
		} else if(zip_code == "") {
			console.log("请输入邮编");
		} else if(address == "") {
			console.log("请输入详细地址");
		} else {
			$.ajax({
				type: "post",
				url: "http://a.91jfk.com/Product/Address/edit_address",
				data: {
					id: id,
					name:name,
					tel:telphone,
					area:area,
					street:street,
					address:address,
					state:state,
				},
				datatype: "json",
				success: function(xhr) {
					if(xhr.code == "1"){
						
					} else {
						window.location.href="point_shop_guanlidizhi.html";
					}
				}
			})

		}
	});

});